package com.paireach.szwolaiyun.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.paireach.szwolaiyun.R;
import com.paireach.szwolaiyun.newFragment.OrderDetailsFragment;
import com.paireach.szwolaiyun.sz_common_widget.activity.BaseActivity;

import szwolaiyun.paireach.com.szwolaizhuang.model.business.DriveOrder;
import szwolaiyun.paireach.com.szwolaizhuang.net.WayBillRequest;

/**
 * 設計圖的A界面，該界面處理以下功能：
 * 1.司機未接單，界面就負責顯示運單詳情，司機可以直接點擊接單按鈕來接單
 * 2.司機已經接單，界面同樣顯示運單詳情，衹不過接單按鈕變成了簽到按鈕，司機點擊該按鈕就可以進行接單簽到
 *
 * @author 鈡偉 2016-08-12
 */
public class Activity_A extends BaseActivity {

    private static final String TAG = "Activity_A";
    private ImageView backView;
    private TextView tvTitleView;

    private StatusJumpReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_a);
        backView = (ImageView) findViewById(R.id.layout_top_back);
        tvTitleView = (TextView) findViewById(R.id.layout_top_change);
        Intent intent = getIntent();
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            DriveOrder driveOrder = (DriveOrder) bundle.getSerializable("DriveOrder");
            if (driveOrder != null) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//                OrderDetailsFragment fragment = OrderDetailsFragment.getInstance(
//                        driveOrder.getAcceptByDriverTime() != null ? true : false, driveOrder);
                OrderDetailsFragment fragment = OrderDetailsFragment.getInstance(driveOrder);
                transaction.replace(R.id.frame, fragment, OrderDetailsFragment.TAG).commit();
            }
        }
        backView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Activity_A.this.finish();
            }
        });
        tvTitleView.setText("运单详情");
        receiver = new StatusJumpReceiver(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
    }

    @Override
    public void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.paireach.szwolaiyun.ACTION_JUMP_NON_STICKY");
        registerReceiver(receiver, filter);
    }

    //签到成功后收到服务器通知，开启Activity_B，如果是224/226/236/238，则再调转到Activity_D,在D中进行fragment的替换。
    private class StatusJumpReceiver extends BroadcastReceiver {

        private String action = "com.paireach.szwolaiyun.ACTION_JUMP_NON_STICKY";
        private String orderNumber;
        private Activity mContext;

        public StatusJumpReceiver(Activity mContext) {
            this.mContext = mContext;
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(action)) {
                //解析数据
                Bundle bundle = intent.getExtras();
                if (bundle != null) {
                    orderNumber = bundle.getString("orderCode");
                    Intent intent1 = new Intent(Activity_A.this, Activity_B.class);
                    intent1.putExtra("orderNumber", orderNumber);
                    //注释掉这一句是因为：MainActivity中也注册了这个广播，防止B被开启2次，
                    //这样A关闭后，MainActivity才可以收到这个广播，在开启B
                    //没有注释掉MainActivity是因为考虑其他的可能会用到
//                    startActivity(intent1);
                    Activity_A.this.finish();
                }
            }
        }
    }
}
